package com.stm.web.controller.toolsTest;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stm.basicData.domain.ToolsTestOption;
import com.stm.basicData.service.IToolsTestOptionService;
import com.stm.common.annotation.Log;
import com.stm.common.core.controller.BaseController;
import com.stm.common.core.domain.AjaxResult;
import com.stm.common.core.page.TableDataInfo;
import com.stm.common.enums.BusinessType;
import com.stm.common.utils.JasperHelper;
import com.stm.framework.util.ShiroUtils;
import com.stm.toolLedger.domain.KfTools;
import com.stm.toolLedger.service.KfToolsService;
import com.stm.toolsTest.domain.KfToolsTestApply;
import com.stm.toolsTest.domain.KfToolsTestInspection;
import com.stm.toolsTest.service.KfToolsTestApplyService;
import com.stm.toolsTest.service.KfToolsTestInspectionService;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 * 
 * @Description 试验记录controller
 * @CreateTime 2019年4月8日 下午5:06:09
 * @CreateBy 林龙
 */
@Controller
@RequestMapping("/toolsTest/testList")
public class TestListController extends BaseController
{
    private String prefix = "toolsTest/testList";
    
    @Value("${stm.templates}") 
    private String templates;

    @Autowired
    private KfToolsTestApplyService kfToolsTestApplyService;
    
    @Autowired
    private KfToolsService kfToolsService;
    
	@Autowired
	private IToolsTestOptionService toolsTestOptionService;
	
	@Autowired
	private KfToolsTestInspectionService kfToolsTestInspectionService;

    /**
     * 
     * @Description 跳转页面
     * @CreateTime 2019年4月4日 上午11:57:34
     * @CreateBy 林龙
     * @return
     */
    @RequiresPermissions("toolsTest:testList:view")
    @GetMapping()
    public String testList(ModelMap mmap)
    {
    	mmap.put("user", ShiroUtils.getSysUser());
        return prefix + "/testList";
    }

    /**
     * 
     * @Description 分页查询
     * @CreateTime 2019年4月4日 上午11:57:25
     * @CreateBy 林龙
     * @param kfToolsTestApply
     * @return
     */
    @RequiresPermissions("toolsTest:testList:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(KfToolsTestApply kfToolsTestApply)
    {
        startPage();
        List<KfToolsTestApply> list = kfToolsTestApplyService.selectAllPageList(kfToolsTestApply);
        return getDataTable(list);
    }
    /**
     * 
     * @Description 新增弹出窗口
     * @CreateTime 2019年4月4日 上午11:57:11
     * @CreateBy 林龙
     * @return
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/testList_add";
    }

    /**
     * 
     * @Description 新增保存 
     * @CreateTime 2019年4月4日 上午11:56:56
     * @CreateBy 林龙
     * @param kfToolsTestApply
     * @return
     */
    @RequiresPermissions("toolsTest:testList:add")
    @Log(title = "试验记录管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody KfToolsTestApply kfToolsTestApply)
    {
    	ShiroUtils.clearCachedAuthorizationInfo();
    	kfToolsTestApply.setDprtcode(ShiroUtils.getSysUser().getDeptId());
    	kfToolsTestApply.setCreateBy(ShiroUtils.getLoginName()); //创建人
    	
        return toAjax(kfToolsTestApplyService.insert(kfToolsTestApply));

    }
    
    
    /**
	 * 修改试验送检
	 */
    @RequiresPermissions("toolsTest:testList:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		KfToolsTestApply kfToolsTestApply = kfToolsTestApplyService.selectByPrimaryKey(id);
		mmap.put("kfToolsTestApply", kfToolsTestApply);
	    return prefix + "/testList_edit";
	}
	
	/**
	 * 修改保存试验送检单
	 * @throws Exception 
	 */
	@RequiresPermissions("toolsTest:testList:edit")
	@Log(title = "试验送检", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(@RequestBody KfToolsTestApply kfToolsTestApply) throws Exception
	{
		kfToolsTestApply.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(kfToolsTestApplyService.update(kfToolsTestApply));
	}
    
    
    /**
     * 
     * @Description 删除
     * @CreateTime 2019年4月4日 上午11:56:27
     * @CreateBy 林龙
     * @param ids
     * @return
     */
    @RequiresPermissions("toolsTest:testList:remove")
    @Log(title = "试验记录管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(kfToolsTestApplyService.deleteByPrimary(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
    
    /**
     * 
     * @Description 查看详情
     * @CreateTime 2019年4月8日 上午9:42:17
     * @CreateBy 林龙
     * @param id
     * @param mmap
     * @return
     */
//    @RequiresPermissions("toolsTest:testList:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("kfToolsTestApply", kfToolsTestApplyService.selectByPrimaryKey(id));
        return prefix + "/testList_detail";
    }
    
    
    
    /**
     * 
     * @Description 提交送检
     * @CreateTime 2019年4月8日 上午9:42:17
     * @CreateBy 林龙
     * @param id
     * @param mmap
     * @return
     * @throws IOException 
     * @throws JRException 
     */
    @PostMapping("/sendInspection")
    @ResponseBody
    public AjaxResult sendInspection(String ids)
    {
        try
        {
            return toAjax(kfToolsTestApplyService.reviewByPrimary(ids,null,null,null));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
    
    
    /**
     * 
     * @Description 生成委托单
     * @CreateTime 2019年4月8日 上午9:42:17
     * @CreateBy 林龙
     * @param id
     * @param mmap
     * @return
     * @throws IOException 
     * @throws JRException 
     */
    @GetMapping("/generateOrder/{id}")
    public void generateOrder(@PathVariable("id") String id, ModelMap mmap,HttpServletRequest request, HttpServletResponse response) throws JRException, IOException 
    {
    	
    	KfToolsTestInspection kfToolsTestInspection=new KfToolsTestInspection();
    	kfToolsTestInspection.setTestId(id);
        List<KfToolsTestInspection> reports = kfToolsTestInspectionService.selectAllPageList(kfToolsTestInspection);
        

        //JRBeanCollectionDataSource通过构造注入collection类型的参数，这里我们用的是list结构  
        JRDataSource jrDataSource = new JRBeanCollectionDataSource(null);
       //构建参数map 
        Map<String, Object> map=new HashMap<String, Object>();
        map.put("sendId", id); 
        String exportFilePath = "送检单";
        String url="";
        
        String osName = System.getProperty("os.name", "");
    	if (osName.startsWith("Windows")) {
    	
    		File file = ResourceUtils.getFile("classpath:templates/jasper/StockAvailableCard.jasper"); //windows可使用
    		url=file.getPath();
    	}else{
    		url=templates+"StockAvailableCard.jasper";
    	}
        

		JasperHelper.showPdf(exportFilePath, url, request, response, map,jrDataSource); //linux上可使用

	//调用工具类
//	JasperHelper.showHtml(exportFilePath , file.getPath(), request,response, map, jrDataSource);        
//	JasperHelper.export("excel", exportFilePath, file, request, response, map, jrDataSource);
    

    }
    /**
     * 
     * @Description 生成报告单
     * @CreateTime 2019年4月8日 上午9:42:17
     * @CreateBy 林龙
     * @param id
     * @param mmap
     * @return
     * @throws IOException 
     * @throws JRException 
     */
    @GetMapping("/reportOrder")
    public void reportOrder(String ids, ModelMap mmap,HttpServletRequest request, HttpServletResponse response) throws JRException, IOException 
    {
    	
    	List<ToolsTestOption> reports = new ArrayList<ToolsTestOption>();    
    	//装载数据  
    	ToolsTestOption report = new ToolsTestOption();  
//    	report.setId("15c851cf-2e09-43fe-beb8-e9e95413245a");//测试为空时，
    	reports.add(report);
    	
    	//JRBeanCollectionDataSource通过构造注入collection类型的参数，这里我们用的是list结构  
    	JRDataSource jrDataSource = new JRBeanCollectionDataSource(reports);
    	//构建参数map 
    	Map<String, Object> map=new HashMap<String, Object>();
    	map.put("recid", "15c851cf-2e09-43fe-beb8-e9e95413245a"); 
    	//子数据源测试
//    	map.put("recid1", reports);
    	
    	String exportFilePath = "报告单";
    	
    	String url="";
    	
    	
    	String osName = System.getProperty("os.name", "");
    	if (osName.startsWith("Windows")) {
    		
    		File file = ResourceUtils.getFile("classpath:templates/jasper/reportOrder.jasper"); //windows可使用
    		url=file.getPath();
    	}else{
    		url=templates+"reportOrder.jasper";
    	}
    	
  
    	JasperHelper.showPdf(exportFilePath, url, request, response, map,jrDataSource); //linux上可使用
    	
    	//调用工具类
//	JasperHelper.showHtml(exportFilePath , file.getPath(), request,response, map, jrDataSource);        
//	JasperHelper.export("excel", exportFilePath, file, request, response, map, jrDataSource);
    	
    	
    }
    
    /**
     * 多选选择试验项目
     */
    @GetMapping("/openSelectTestProject")
    public String openSelectTestProject( )
    {
    	
    	return "open/openSelectTestProject";
    }
    
    /**
     * 多选选择工具
     */
    @GetMapping("/openSelectTools/{applyDeptId}")
    public String openSelectTools(@PathVariable("applyDeptId") String applyDeptId,ModelMap mmap )
    {
    	mmap.put("applyDeptId", applyDeptId);
    	return "open/openSelectTools";
    }
    
    /**
     * 
     * @Description 根据试验id查询试验项目列表
     * @CreateTime 2019年4月4日 上午11:57:25
     * @CreateBy 林龙
     * @param kfTools
     * @return
     */
    @PostMapping("/toolsTestProjectList")
    @ResponseBody
    public TableDataInfo toolsTestProjectList(String ids)
    {
        startPage();
        List<ToolsTestOption> list = toolsTestOptionService.toolsTestProjectList(ids);
        return getDataTable(list);
    }
    
    /**
     * 
     * @Description 根据工具id查询工具
     * @CreateTime 2019年4月4日 上午11:57:25
     * @CreateBy 林龙
     * @param kfTools
     * @return
     */
    @PostMapping("/toolsList")
    @ResponseBody
    public TableDataInfo toolsList(String ids)
    {
        startPage();
        List<KfTools> list = kfToolsService.selectAllPageListByIds(ids);
        return getDataTable(list);
    }
    
    /**
	 * 试验样品列表
	 */
	@PostMapping("/testSampleList")
	@ResponseBody
	public TableDataInfo testSampleList(KfToolsTestInspection kfToolsTestInspection)
	{
		startPage();
        List<KfToolsTestInspection> list = kfToolsTestInspectionService.selectAllPageList(kfToolsTestInspection);
		return getDataTable(list);
	}
  
}